package parser

import (
	"learn/crawler/engine"
	"learn/crawler/model"
	"learn/crawler/util"
	"regexp"
)

var listRe = regexp.MustCompile(`<li>[^<]*<div class="job-primary">[\s\S]*?</li>`)
var nameRe = regexp.MustCompile(`<div class="job-title">([^<]+)</div>`)
var infoRe = regexp.MustCompile(`<p>([^<]+)<em class="vline"></em>([^<]+)<em class="vline"></em>[^<]+</p>`)
var salaryRe = regexp.MustCompile(`<span class="red">([^<]+)</span>`)
var comRe = regexp.MustCompile(`<a href="[^"]+" ka="search_list_company_1_custompage" target="_blank">([^<]+)</a>`)

func JobListParse(contents []byte) engine.ParseResult {
	matches := listRe.FindAllSubmatch(contents, -1)

	result := engine.ParseResult{}
	for _, m := range matches {
		c := m[0]
		job := model.Job{}
		job.Name = util.ExtractString(c, nameRe)
		job.Salary = util.ExtractString(c, salaryRe)
		job.Company = util.ExtractString(c, comRe)
		infoMatches := infoRe.FindAllSubmatch(c, -1)
		for _, im := range infoMatches {
			job.Address = string(im[1])
			job.WorkTime = string(im[2])
		}
	}

	return result
}
